package com.travelms.service.commonuserpage;

import com.travelms.pojo.Train;
import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class BuyTicket {
    //用stack来表示层级结构以便更好返回上一级
    DBFind dbFind = new DBFind();
    ArrayList<Train> trainArrayList = null;

    public BuyTicket(String username) throws SQLException, IOException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);

        while (true) {
            if (current == 0) {
                System.out.println("1.输入票的地点查询");
                System.out.println("2.输入出发地和目的地查询");
                System.out.println("3.退出");
            }
            while (true){
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)){//输入效验
                    System.out.println("输入错误");
                } else if(!(s.compareTo("0") > 0 && s.compareTo("4") < 0)) {
                    System.out.println("输入错误");
                }else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if(current == 3){

                //为3则退出
                break;
            }else if(current == 1){
                while (true) {
                    System.out.println("输入exit退出输入!!!");
                    System.out.println("请输入地址：");

                    String dest = sc.nextLine();
                    if ("exit".equals(dest)){
                        break;
                    }
                    trainArrayList =  dbFind.findDestTicket(dest);
                    if (trainArrayList.isEmpty()){
                        System.out.println("没有该地址的车票");
                        continue;
                    }
                    int start = trainArrayList.get(0).getId();
                    int end = trainArrayList.get(trainArrayList.size()-1).getId();
                    trainArrayList.forEach(System.out::println);
                    String opt = null;
                    while (true) {
                        System.out.println("请输入你要订购的车票id：");
                        opt = sc.nextLine();

                        if (DataEffect.isNumber(opt) && (Integer.parseInt(opt) >= start && Integer.parseInt(opt) <= end)){
                            break;
                        }
                        System.out.println("输入错误");
                    }
                    int i = dbFind.orderTicket(opt,username);
                    if(i == 1){
                        System.out.println("余额不足，请检查余额");
                        break;
                    } else if (i == 2) {
                        System.out.println("车票数量不足,请换一张车票");
                    } else if (i == 3) {
                        System.out.println("购买失败");
                        break;
                    }else {
                        System.out.println("购买成功");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();
            }else if(current == 2){
                //输入出发地和目的地查询
                while (true) {
                    System.out.println("输入exit退出输入!!!");
                    System.out.println("请输入出发地：");
                    String start = sc.nextLine();
                    if (start.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if ("exit".equals(start)){
                        break;
                    }
                    System.out.println("请输入目的地：");
                    String end = sc.nextLine();
                    if (end.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if ("exit".equals(end)){
                        break;
                    }
                    trainArrayList =  dbFind.findStartToEndTicket(start,end);
                    if (trainArrayList.isEmpty()){
                        System.out.println("没有该地址的车票");
                        continue;
                    }
                    int startNum = trainArrayList.get(0).getId();
                    int endNum = trainArrayList.get(trainArrayList.size()-1).getId();
                    trainArrayList.forEach(System.out::println);
                    String opt = null;
                    while (true) {
                        System.out.println("请输入你要订购的车票id：");
                        opt = sc.nextLine();

                        if (DataEffect.isNumber(opt) && (Integer.parseInt(opt) >= startNum && Integer.parseInt(opt) <= endNum)){
                            break;
                        }
                        System.out.println("输入错误");
                    }
                    int i = dbFind.orderTicket(opt,username);
                    if(i == 1){
                        System.out.println("余额不足，请检查余额");
                        break;
                    } else if (i == 2) {
                        System.out.println("车票数量不足,请换一张车票");
                    } else if (i == 3) {
                        System.out.println("购买失败");
                        break;
                    }else {
                        System.out.println("购买成功");
                        break;
                    }

                }

                stack.pop();
                current = stack.peek();
            }


        }
    }
}
